package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.*;
import com.lovo.pis.mapper.*;
import com.lovo.pis.service.IStockGoodsService;
import com.lovo.pis.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class StockGoodsServiceImpl extends ServiceImpl<StockGoodsMapper, StockGoodsBean> implements IStockGoodsService {

    @Autowired
    StockGoodsMapper stockGoodsMapper;
    @Autowired
    BuyOrderMapper buyOrderMapper;
    @Autowired
    BuyGoodsMapper buyGoodsMapper;
    @Autowired
    BuyCarGoodsMapper buyCarGoodsMapper;
    @Autowired
    SellOrderMapper sellOrderMapper;
    @Autowired
    SellCarGoodsMapper sellCarGoodsMapper;
    @Autowired
    SellGoodsMapper sellGoodsMapper;
    @Autowired
    LogUtil logUtil;

    @Override
    public IPage<StockGoodsBean> queryAllStockGoods(PageBean<StockGoodsBean> pageInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pageInfo.getData().getName() != null && pageInfo.getData().getName() != "") {
            queryWrapper.like("name", pageInfo.getData().getName());
        }
        if (pageInfo.getMinNumber() > 0) {
            queryWrapper.ge("stock_number", pageInfo.getMinNumber());
        }
        if (pageInfo.getMaxNumber() > 0) {
            queryWrapper.le("stock_number", pageInfo.getMaxNumber());
        }
        if (pageInfo.getIsSurePrice() == 1) {
            queryWrapper.eq("sell_price", 0);
        }
        if (pageInfo.getData().getType() != null && pageInfo.getData().getType() != "") {
            queryWrapper.eq("type", pageInfo.getData().getType());
        }
        return stockGoodsMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()), queryWrapper);
    }

    @Override
    public List<String> queryType() {
        return stockGoodsMapper.selectType();
    }

    @Override
    public StockGoodsBean queryById(int id) {
        return stockGoodsMapper.selectById(id);
    }

    @Override
    public void changeById(StockGoodsBean stockGoodsBean) {
        stockGoodsMapper.updateById(stockGoodsBean);
    }

    @Override
    @Transactional
    public void inStock(String supplierName, String buyer, int carId) {
        if (supplierName != null && supplierName!="") {
            //采购单表新增
            BuyOrderBean buyOrderBean = new BuyOrderBean();
            buyOrderBean.setBuyer(buyer);
            buyOrderBean.setSupplierName(supplierName);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
            LocalDate localDate = LocalDate.now();
            String nowTimeStr = dtf.format(localDate);
            buyOrderBean.setCreateTime(nowTimeStr);
            buyOrderMapper.insert(buyOrderBean);
            //采购商品明细表新增
            int orderId = buyOrderBean.getId();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("car_id", carId);
            List<BuyCarGoodsBean> buyCarGoodsList = buyCarGoodsMapper.selectList(queryWrapper);
            buyGoodsMapper.insertBuyGoods(buyCarGoodsList, orderId);
            //判断库存是否存在同类型商品
            for (int i = 0; i < buyCarGoodsList.size(); i++) {
                StockGoodsBean stockGoodsBean = stockGoodsMapper.selectById(buyCarGoodsList.get(i).getId());
                if (stockGoodsBean != null) {
                    stockGoodsBean.setStockNumber(stockGoodsBean.getStockNumber() + buyCarGoodsList.get(i).getNumber());
                    stockGoodsBean.setBuyPrice(buyCarGoodsList.get(i).getPrice());
                    stockGoodsMapper.updateById(stockGoodsBean);
                } else {
                    stockGoodsMapper.insertStock(buyCarGoodsList.get(i));
                }
            }
            //根据采购车carId清空采购车
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("car_id", carId);
            buyCarGoodsMapper.delete(queryWrapper1);
            //记录日志
            logUtil.recordLog(carId,"购买入库");
        }
    }

    @Override
    @Transactional
    public void outStock(String customName, String seller, double price, String address, int carId) {
        if (customName!=null && customName!="" && address!=null && address!=""){
            //销售单表新增
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
            LocalDate localDate = LocalDate.now();
            String nowTimeStr = dtf.format(localDate);
            SellOrderBean sellOrderBean = SellOrderBean.builder().address(address).totalPrice(price).seller(seller).customName(customName).createTime(nowTimeStr).build();
            sellOrderMapper.insert(sellOrderBean);
            //销售商品明细表新增
            int orderId = sellOrderBean.getId();
            List<SellCarGoodsBean> sellCarGoodsList = sellCarGoodsMapper.selectAllSellCarGoods(carId);
            sellGoodsMapper.insertSellGoods(sellCarGoodsList,orderId);
            //库存扣除
            for (int i = 0; i < sellCarGoodsList.size(); i++) {
                StockGoodsBean stockGoodsBean = stockGoodsMapper.selectById(sellCarGoodsList.get(i).getId());
                    stockGoodsBean.setStockNumber(stockGoodsBean.getStockNumber() - sellCarGoodsList.get(i).getNumber());
                    stockGoodsMapper.updateById(stockGoodsBean);
            }
            //根据采购车carId清空采购车
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("car_id", carId);
            sellCarGoodsMapper.delete(queryWrapper1);
            //记录日志
            logUtil.recordLog(carId,"销售出库");
        }
    }


}
